<?php

	class PDatabaseConnection
	{
		var $name;
		var $host;
		var $port;
		var $username;
		var $password;
		var $dbname;
		
		var $connected;
		
		var $result;
		var $num_results;
		
		var $lastQuery;
		
		
		function PDatabaseConnection($name, $host, $dbname, $user, $pass)
		{
			$this->name = $name;
			$this->host = $host;
			$this->username = $user;
			$this->password = $pass;
			$this->dbname = $dbname;
			
			$this->connected = false;
			
			$this->result = array();
		}
		
		public function connect()
		{
			if(!$this->connected)
			{
			    $myconn = @mysql_connect($this->host, $this->username, $this->password);
			    
			    if($myconn)
			    {
			    	mysql_set_charset('utf8', $myconn);
				$seldb = @mysql_select_db($this->dbname, $myconn);
				if($seldb)
				{
				    $this->connected = true; 
				    return true; 
				}
				else
				{
				    return false; 
				}
			    }
			    else
			    {
				return false; 
			    }
			}
			else
			{
			    return true; 
			}
		}
		
		public function disconnect()
		{
			if($this->connected)
			{
				if(@mysql_close())
				{
					$this->connected = false; 
					return true; 
				}
				else
				{
					return false; 
				}
			}
		}
		
		function tableExists($table)
		{
			$tablesInDb = $this->executeQuery('SHOW TABLES FROM '. $this->dbname.' LIKE "' . $table . '"');
			if($tablesInDb)
			{
			    if(mysql_num_rows($tablesInDb) == 1)
			    {
				return true;
			    }
			    else
			    { 
				return false;
			    }
			}
		}
		
		function executeQuery($query)
		{
			$this->lastQuery = $query;
			return @mysql_query($query);
		}
		
		public function select($table, $rows = '*', $where = null, $order = null)
		{
			$this->result = array();
			
			$q = 'SELECT '.$rows.' FROM '.$table;
			
			if($where != null)
			    $q .= ' WHERE '.$where;
			
			if($order != null)
			    $q .= ' ORDER BY '.$order;
			
			if($this->tableExists($table))
			{
				$query = $this->executeQuery($q);
				
				if($query)
				{
				    $this->numResults = mysql_num_rows($query);
				    for($i = 0; $i < $this->numResults; $i++)
				    {
					$r = mysql_fetch_array($query);
					$key = array_keys($r);
					for($x = 0; $x < count($key); $x++)
					{
					    // Sanitizes keys so only alphavalues are allowed
					    if(!is_int($key[$x]))
					    {
						if(mysql_num_rows($query) > 1)
						    $this->result[$i][$key[$x]] = $r[$key[$x]];
						else if(mysql_num_rows($query) < 1)
						    $this->result = null;
						else
						    $this->result[0][$key[$x]] = $r[$key[$x]];
					    }
					}
				    }            
				    return true;
				}
				else
				{
				    return false;
				}
			}
			else
			{
				return false;
			}
		}
		
		public function insert($table, $values, $rows = null)
		{
			if($this->tableExists($table))
			{
				$insert = 'INSERT INTO ' . $table;
				
				if($rows != null)
				{
					$insert .= ' (' . $rows . ')'; 
				}
				
				for($i = 0; $i < count($values); $i++)
				{
					if($values[$i] == "" or is_string($values[$i]))
					    $values[$i] = '"' . $values[$i] . '"';
				}
				
				$values = implode(',', $values);
				$insert .= ' VALUES ('.$values.')';
				$ins = $this->executeQuery($insert);
				
				if($ins)
				{
					return true; 
				}
				else
				{
					return false; 
				}
			}
		}
		
		public function delete($table, $where = null)
		{
			if($this->tableExists($table))
			{
				if($where == null)
				{
					$delete = 'DELETE ' . $table; 
				}
				else
				{
					$delete = 'DELETE FROM '.$table.' WHERE '.$where; 
				}
				
				$del = $this->executeQuery($delete);
				
				if($del)
				{
					return true; 
				}
				else
				{
					return false; 
				}
			}
			else
			{
			    return false; 
			}
		}
		
		public function update($table, $rows, $where)
		{
			if($this->tableExists($table))
			{
				// Parse the where values
				// even values (including 0) contain the where rows
				// odd values contain the clauses for the row
				
				for($i = 0; $i < count($where); $i++)
				{
					if($i%2 != 0)
					{
						if(is_string($where[$i]))
						{
							if($where[$i+1] != null)
								$where[$i] = '"'.$where[$i].'" AND ';
							else
								$where[$i] = '"'.$where[$i].'"';
						}
					}
				}
				$where = implode('=',$where);
				
				$update = 'UPDATE '.$table.' SET ';
				$keys = array_keys($rows);
				
				for($i = 0; $i < count($rows); $i++)
				{
					if($rows[$keys[$i]] == "" or is_string($rows[$keys[$i]]))
					{
						$update .= $keys[$i].'="'.$rows[$keys[$i]].'"';
					}
					else
					{
						$update .= $keys[$i].'='.$rows[$keys[$i]];
					}

					// Parse to add commas
					if($i != count($rows)-1)
					{
						$update .= ',';
					}
				}
				
				$update .= ' WHERE '.$where;
				$query = $this->executeQuery($update);
				
				if($query)
				{
					return true;
				}
				else
				{
					return false;
				}
			}
			else
			{
				return false;
			}
		}
		
		function getLastID()
		{
			return mysql_insert_id();
		}
		
		function dieLastQuery()
		{
			die ("Puzzle :: Data Manager :: Forced die while executing query: " . $this->lastQuery);
		}

	}
?>
